package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.Impl.ScrollDaoImpl;
import com.ysd.dao.Impl.UserDaoImpl;
import com.ysd.entity.NewsCatalogue;
import com.ysd.entity.User;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminUserServlet extends BaseServlet {
	/****
	 * 留言列表
	 *  localhost:8080/huaduoyoupin/adminUser?reqName=getUser
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDaoImpl uDao=new UserDaoImpl();//创建实现类对象
		
		String status=request.getParameter("status");
		int page=0;
	    if(request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
	    	page=Integer.parseInt(request.getParameter("page"));
		}
	    int size=0;
	    if(request.getParameter("size")!=null && !request.getParameter("size").equals("")) {
	    	size=Integer.parseInt(request.getParameter("size"));
		}
		List<User> uList=uDao.getUserByStatus(status,page,size);//获得留言内容
		
		response.setCharacterEncoding("UTF-8");//设置响应编码集格式
		PrintWriter out=response.getWriter();//out对象
		
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(uList);//将uList转化为json格式的字符串
		out.print(res);
		
		
		out.flush();
		out.close();
		
	}
	//分页查询
	//localhost:8080/huaduoyoupin/adminUser?reqName=getAllUser
	public void getAllUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDaoImpl uDao=new UserDaoImpl();//创建实现类对象
		
		String status=request.getParameter("status");
		List<User> uList=uDao.getUserByStatusPage(status);//获得留言内容
		
		response.setCharacterEncoding("UTF-8");//设置响应编码集格式
		PrintWriter out=response.getWriter();//out对象
		
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(uList.size());//将uList转化为json格式的字符串
		out.print(res);
		
		
		out.flush();
		out.close();
		
	}
  //根据id查询
	// localhost:8080/huaduoyoupin/adminUser?reqName=getUserById
	public void getUserById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDaoImpl uDao=new UserDaoImpl();//创建实现类对象
		int id=0;
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")) {
			id=Integer.parseInt(request.getParameter("id"));
		}
		
		User user=uDao.getUserById(id);//获得留言内容
		
		response.setCharacterEncoding("UTF-8");//设置响应编码集格式
		PrintWriter out=response.getWriter();//out对象
		
		
		Gson gson=new Gson();//创建gson对象
		
		String res=gson.toJson(user);//将uList转化为json格式的字符串
		out.print(res);
		
		
		out.flush();
		out.close();
		
	}
	/**
	 * 修改留言状态
	 * localhost:8080/huaduoyoupin/adminUser?reqName=updateMessageById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateMessageById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置响应编码集格式
		PrintWriter out=response.getWriter();//out对象
	
		UserDaoImpl uDao=new UserDaoImpl();//创建实现类对象
		
		int id=Integer.parseInt(request.getParameter("id"));
		User user=uDao.getUserById(id);
		if(user.getStatus().equals("已读")||user.getStatus().equals("已回复")) {
			out.print(Result.toClient(true, "不需要修改",""));
		}else {
			user=new User(id, "已读",null);
			int n=uDao.updateMessageById(user);//获得留言内容
			if(n>0) {
				out.print(Result.toClient(true, "修改成功",""));
			}else {
				out.print(Result.toClient(false, "修改失败",""));
			}
		}
		
		
		
		out.flush();
		out.close();
		
	}
	//修改回复
	//localhost:8080/huaduoyoupin/adminUser?reqName=updateMessageReplyById
	public void updateMessageReplyById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");//设置响应编码集格式
		PrintWriter out=response.getWriter();//out对象
	
		UserDaoImpl uDao=new UserDaoImpl();//创建实现类对象
		
		int id=Integer.parseInt(request.getParameter("id"));
		String reply=request.getParameter("reply");
		User user=new User(id, null, reply);
		int n=uDao.updateMessageReplyById(user);//获得留言内容
		if(n>0) {
			out.print(Result.toClient(true, "回复成功",""));
		}else {
			out.print(Result.toClient(false, "回复失败",""));
		}
		
		
		out.flush();
		out.close();
		
	}
	//删除
	//localhost:8080/huaduoyoupin/adminUser?reqName=delUser
	public void delUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int id=Integer.parseInt(request.getParameter("id"));//接收前台传来的参数
		UserDaoImpl uDao=new UserDaoImpl();//创建实现类
		
		response.setCharacterEncoding("UTF-8");//设置编码集格式
		PrintWriter out=response.getWriter();
		//调用实现类的删除方法，返回受影响行数
		int n=uDao.delUser(id);
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		
		out.flush();
		out.close();
	}
	//批量删除
	//localhost:8080/huaduoyoupin/adminUser?reqName=deleteMassageByIds
	public void deleteMassageByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserDaoImpl uDao=new UserDaoImpl();//创建实现类
		
		response.setCharacterEncoding("UTF-8");//设置编码集格式
		PrintWriter out=response.getWriter();
		String ids=request.getParameter("ids");
		int n=uDao.deleteMassageByIds(ids);
		//调用实现类的删除方法，返回受影响行数
		if(n>0) {
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		
		out.flush();
		out.close();
	}
}
